const API = require('../../../../utils/api')
const app = getApp()
const dayjs = require('../../../../utils/dayjs.min')
const {openMmap} = require("../../../../utils/util")
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo:{},
        housingList: [],
        companyData: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if( options && options.inviterUserId ){
            app.globalData.inviterUserId = options.inviterUserId
            this.setData({
                userInfo: {
                    nickName: options.nickName,
                    wxPhone: options.wxPhone,
                    headimgUrl: options.headimgUrl
                }
            })
        }else{
            this.setData({
                userInfo: app.globalData.userInfo
            })
        }
        this.get_article_info_housing()
        this.get_applet_company_data()
        this.get_artcle_detail( options.id )
    },
    /**
     * 打开地图
     */
    locationEvent(event) {
        console.log(event)
        // openMmap(this.data.item.location)
      },
    /**
     * 接口 - 获取精品房源 企业信息
     */
    async get_article_info_housing () {
       const res = await API.article_info_housing()
       if (res.code != 200) return;
       this.setData({ housingList: res.rows })
    },
    /**
     * 接口 - 获取 用户的企业信息
     */
    async get_applet_company_data () {
        const userInfo = app.globalData.userInfo
        if (!userInfo || !userInfo.id) return;
        const res = await API.applet_company_data({
            userId: userInfo.id
        })
        if (res.code != 200) return;
        const company = res.data.data
        company.establishTime = company.establishTime ? dayjs(company.establishTime).format('YYYY年MM月DD日') : ''
        console.log("company-------------------", company)
        this.setData({ 
            companyData: company,
            userInfo: userInfo
        })
    },
    /**
     * 接口 - 获取文章详情
     */
    get_artcle_detail:function( id ){
        API.get_artcle_detail({
            id:id
        }).then( res => {
            if( res.code != 200 ){
                return
            }
            let detail = res.data
            detail.content = detail.content.replace(/\<img/gi, '<img style="max-width:100%;height:auto"')
            this.setData({
                detail: detail
            })
        })
    },

    /**
     * 打电话
     */
    callTel:function(e){
        wx.makePhoneCall({
          phoneNumber: e.currentTarget.id,
        })
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        let that = this
        return {
            title: that.data.detail.title,
            path: '/pages/home/marketing/news/detail?id=' + that.data.detail.id + '&inviterUserId=' + app.globalData.userInfo.id + '&nickName=' + that.data.userInfo.nickName + '&wxPhone=' + that.data.userInfo.wxPhone + '&headimgUrl=' + that.data.userInfo.headimgUrl,
            imageUrl: that.data.detail.logoImg,
        }
    }
})